// pages/SuSong/companyDetail/comDetail.js
import request from '../../../utils/config.js';
import util from '../../../utils/util.js';
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    custInfo:{},
    stepName: '',
    showStep: false, //选择案件后显示 模块
    isHave: true,  //下一阶段 
    isEmpty: false,
    showDepartment: false,
    tabSlt1: false,
    tabSlt2: false,
    tabSlt3: false,
    departmentList: ['本人继续跟进', '转电销二次销售','转面销跟进'],
    memo:"",
    sltA: false,
    sltB:false,
    sltC:false,
    grade:""
  },

  //选择等级
  selectStartA: function(){
    this.setData({
      sltA: true,
      sltB: false,
      sltC: false,
      grade: 3
    });
  },
  selectStartB: function () {
    this.setData({
      sltB: true,
      sltA: false,
      sltC: false,
      grade: 2
    });
  },
  selectStartC: function () {
    this.setData({
      sltC: true,
      sltA: false,
      sltB: false,
      grade: 1
    });
  },

  goHave: function() {
    this.setData({
      tabSlt1: true,
      tabSlt2: false,
      tabSlt3: false,
      showDepartment: false,
      showStep: true,
      isEmpty: false,
      isHave: true,
      memo:"有合作案件"
    })
  },

  goZhenDu: function () {
    this.setData({
      tabSlt1: false,
      tabSlt2: false,
      tabSlt3: true,
      showDepartment: false,
      showStep: true,
      isEmpty: false,
      isHave: true,
      memo: "有诊断案件"
    })
  },

  goEmpty: function() {
    this.setData({
      tabSlt1: false,
      tabSlt2: true,
      tabSlt3: false,
      showDepartment: true,
      showStep: true,
      isHave: false,
      isEmpty: true,
      memo: "无案件"
    })
  },
  isShowDepartment() {
    let _this=this;
    wx.showActionSheet({
      itemList: _this.data.departmentList,
      success: function (res) {
        console.log(res.tapIndex)
        _this.setData({
          stepName: _this.data.departmentList[res.tapIndex],
        })
      },
      fail: function (res) {
        console.log(res.errMsg)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  //备注触发
  memoChange: function(e){
    this.setData({
      memo: e.detail.value
    });
  },
  //点击收藏
  collect: function (e) {
    let info = this.data.custInfo;
    let _this = this;
    if (info.custVo.isFavor == "Y") {
      request.requestMisApi('delete', "favorCusts/" + info.custVo.custId, { custId: info.custVo.custId }, (res) => {
        console.log("取消收藏成功=", res);
        info.custVo.isFavor = "N";
        app.globalData.custInfo = info;
        _this.setData({
          custInfo: info
        });
      }, (res) => {
        wx.hideLoading()
        console.log("取消收藏失败=", res)
      });
    } else if (info.custVo.isFavor == "N") {
      request.requestMisApi('post', "favorCusts/" + info.custVo.custId, { custId: info.custVo.custId }, (res) => {
        console.log("收藏成功=", res);
        info.custVo.isFavor = "Y";
        app.globalData.custInfo = info;
        _this.setData({
          custInfo: info
        });
      }, (res) => {
        wx.hideLoading()
        console.log("收藏失败=", res)
      });
    }

  },
  //点击提交信息
  subInfo: function(){
    let info = this.data.custInfo;
    if (info.custVo.statusFlag == 'xscg'){
      let hasCaseMsg = "";
      let action = "";
      if (this.data.tabSlt1){
        hasCaseMsg = "有合作案件";
        action ="转客服跟进";
      } else if (this.data.tabSlt3){
        hasCaseMsg = "有诊断案件";
        action = "转客服跟进";
      }else if (this.data.tabSlt2){
        hasCaseMsg = "无案件";
        action = this.data.stepName;
        if (!this.data.stepName){
          wx.showModal({
            title: '提示',
            content: '请选择下一个阶段',
            confirmText: '知道了',
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
              } else if (res.cancel) {
              }
            }
          })
          return;
        }
       
      }

      if(!this.data.memo){
        wx.showModal({
          title: '提示',
          content: '请填写备注',
          confirmText: '知道了',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
            } else if (res.cancel) {
            }
          }
        })
        return;
      }

      let desc = hasCaseMsg + "," + this.data.memo;
      if (hasCaseMsg == this.data.memo){
        desc = hasCaseMsg;
      }
      let bodyInfo = {
        desc: desc,
        action: action,
        grade: this.data.grade
      }
      let _this = this;
      wx.showLoading({ title: '加载中' })
      request.requestMisApi('put', "flyWorkPlace/custom/" + info.custVo.custId, bodyInfo, (res) => {
        console.log("提交销售成果成功=", res);
        wx.hideLoading()
        wx.navigateBack({})
      }, (res) => {
        wx.hideLoading()
        console.log("提交销售结果失败=", res)
      });
    } else if (info.custVo.statusFlag == 'jdjl'){
      let bodyInfo = this.data.memo
      let _this = this;
      wx.showLoading({ title: '加载中' })
      request.requestMisApi('post', "custs/" + info.custVo.custId + "/devRecords", bodyInfo, (res) => {
        console.log("提交客户交往记录成功=", res);
        wx.hideLoading()
        wx.navigateBack({})
      }, (res) => {
        wx.hideLoading()
        console.log("提交客户交往记录失败=", res)
      });
    }
  },

  //拨打电话
  callPhone: function (e) {
    let pho = e.currentTarget.dataset.phone;
    wx.makePhoneCall({
      phoneNumber: pho
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let info = app.globalData.custInfo;
    console.log(info)
    let startImgList = [
      { index: 1, slt: false },
      { index: 2, slt: false },
      { index: 3, slt: false }
    ];
    if (info.custVo.grade == 1) {
      startImgList = [
        { index: 1, slt: true },
        { index: 2, slt: false },
        { index: 3, slt: false }
      ]
    } else if (info.custVo.grade == 2) {
      startImgList = [
        { index: 1, slt: true },
        { index: 2, slt: true },
        { index: 3, slt: false }
      ]
    } if (info.custVo.grade == 3) {
      startImgList = [
        { index: 1, slt: true },
        { index: 2, slt: true },
        { index: 3, slt: true }
      ]
    }
    this.setData({
      startImgList: startImgList,
      grade: info.custVo.grade,
      custInfo: info,
    });
    if (info.custVo.grade == 3) {
      this.setData({
        sltA: true,
      });
    } else if (info.custVo.grade == 2) {
      this.setData({
        sltB: true,
      });
    } else if (info.custVo.grade == 1) {
      this.setData({
        sltC: true,
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})